var arr = [
	"img/game/game-tip-bg.png",
	"img/game/game-feiji.png",
	"img/game/game-yunshi1.png",
	"img/game/game-yunshi2.png",
	"img/game/game-yunshi3.png",
	"img/game/game-yunshi4.png",
	"img/game/game-yunshi5.png",
	"img/game/game-yunshi6.png",
	"img/game/game-yunshi7.png",
	"img/game/game-yunshi8.png",
	"img/game/game-yunshi9.png",
	"img/game/game-yunshi10.png",
	"img/game/game-yunshi11.png",
	"img/game/game-yunshi12.png",
	"img/game/game-yunshi13.png",
	"img/game/game-yunshi14.png",
	"img/game/game-yunshi15.png",
	"img/game/game-yunshi16.png",
	"img/game/game-yunshi17.png",
	"img/game/game-yunshi18.png",
	"img/game/game-yunshi19.png",
	"img/game/game-yunshi20.png",
	"img/game/game-yunshi21.png",
	"img/game/game-yunshi22.png",
	"img/game/game-yunshi23.png",
	"img/game/game-yunshi24.png",
	"img/game/game-yunshi25.png",
	"img/game/game-yunshi26.png",
	"img/game/game-nengliang.png",
	"img/game/game-tip-yunshi.png",
	"img/game/game-tip-liaojie.png",
	"img/game/zailaiyici.png",
	"img/game/fanhuishouye.png",
	"img/game/game1-bg.jpg",
	"img/game/game2-bg.jpg",
	"img/game/game3-bg.jpg",
	"img/game/game1-succes-bg.jpg",
	"img/game/game-succes-chakan-pic1.png",
	"img/game/game-succes-chakan-pic2.png",
	"img/game/game-succes-chakan-pic3.png",
	"img/game/game-succes-chakan.png",
	"img/game/game-chakan-bg.png",
	"img/game/game-chakan-pic1.png",
	"img/game/game-chakan-pic2.png",
	"img/game/game-chakan-pic3.png",
	"img/game/game-chakan-pic4.png",
	"img/game/game-chakan-pic5.png",
	"img/game/game-chakan-pic6.png",
	
];
			
//实例化预加载对象
var queue = new createjs.LoadQueue();
			
//监听加载事件
queue.on("fileload",function(e){
//	console.log(queue.progress);
	var jindu = Math.round(parseFloat(queue.progress)*100);
	document.getElementsByClassName("loading-text")[0].children[0].innerHTML = jindu  +"%";
	document.getElementsByClassName("loading-text")[0].style.top = (100 - jindu -2) + "%";
	document.getElementsByClassName("loading-feiji")[0].style.top = (100 - jindu) + "%";
	document.getElementsByClassName("loading-xian")[0].style.top = (100 - jindu + 5) + "%";
	
})
			
//完全加载完毕
queue.on("complete",function(){
//	console.log("加载完成");

	document.getElementsByClassName("gameload")[0].classList.add("gameloaded");
	
})
			
//批量加载
queue.loadManifest(arr);



var feiji = document.getElementById("game-feiji");

//得到新坐标
function newxy(e){
	//手指坐标
	var x = parseInt(e.touches[0].pageX);
	var y = parseInt(e.touches[0].pageY);
	
	return [x,y];
}




//飞机跟随手指(触摸开始)
//function feijimove(e){
//	
//	if(e){
////		console.log(e.touches[0]);
//		var arr = newxy(e);
//		
////		console.log(gamefeiji.Top);
//		
//		var feitime = setInterval(function(){
//			var feix = parseInt(window.getComputedStyle(feiji).left) + (parseInt(window.getComputedStyle(feiji).width)/2);
//			var feiy = parseInt(window.getComputedStyle(feiji).top) + (parseInt(window.getComputedStyle(feiji).height)/2);
//			if((arr[0] == feix)&&(arr[1] == feiy)){
//				clearInterval(feitime);
//			}
//			if(arr[0]<feix){
//				feiji.style.left = parseInt(window.getComputedStyle(feiji).left) - 1 + "px";
//			}
//			else if(arr[0]>feix){
//				feiji.style.left = parseInt(window.getComputedStyle(feiji).left) + 1 + "px";
//			}
//			
//			if(arr[1]<feiy){
//				feiji.style.top = parseInt(window.getComputedStyle(feiji).top) - 1 + "px";
//			}
//			else if(arr[1]>feiy){
//				feiji.style.top = parseInt(window.getComputedStyle(feiji).top) + 1 + "px";
//			}
//		},10);
//		
//	}
//	else{
//		if(feitime){
//			clearInterval(feitime);
//		}
//	}
//}

//飞机跟随手指（移动）
function feijimovem(e){
	if(e){
//		console.log(e.touches[0]);
		var arr = newxy(e);
		
		var feil = parseInt(window.getComputedStyle(feiji).left);
		var feit = parseInt(window.getComputedStyle(feiji).top);
		var feih = parseInt(window.getComputedStyle(feiji).height);
		var feiw = parseInt(window.getComputedStyle(feiji).width);
		var Left = arr[0] - (feiw/2);
		var Top = arr[1] - (feih/2);
		var web = document.getElementsByClassName("web")[0];
		if(Left>=0&&Left<=(web.offsetWidth - feiw)){
			feiji.style.left = Left + "px";
		}
		
		if(Top>=0&&Top<=(web.offsetHeight - feih)){
			feiji.style.top = Top + "px";
		}
	}
	else{
		
	}
}

var game1 = document.getElementsByClassName("game1")[0];



//陨石下降
var yunshi = document.getElementsByClassName("game-yunshi");

var yunshistop = new Array();
var yn=0;
function yunshimove(yun){
	var ra = Math.random()*(320 - parseInt(getComputedStyle(yun).width));
	yun.style.left =  ra  + "px";
	yun.style.top = "-100%";
	
	s = Math.random();
	var num = yn;
	yunshistop[yn] = setInterval(function(){
		if(parseInt(yun.style.top)>=parseInt(getComputedStyle(game1).height)){
			yun.remove();
			clearInterval( yunshistop[num]);
		}
		yun.style.top = parseInt(yun.style.top) + 1 + "px";
	},20*s);
	yn++;
}

//能量降落
var nengliang = document.getElementsByClassName("nengliang");


function nengliangmove(neng){
	neng.style.left = Math.random()*(320-parseInt(getComputedStyle(neng).width)) + "px";
	neng.style.top = "-100%";
	
	setInterval(function(){
		neng.style.top = parseInt(neng.style.top) + 1 + "px";
	},20);
}

//碰撞检测
function pengzhuang(){
	
	for(let j=0;j<yunshi.length;j++){
//		var Top = parseInt(window.getComputedStyle(yunshi[j]).top);
//		var Left = parseInt(window.getComputedStyle(yunshi[j]).left);
//		var w = parseInt(window.getComputedStyle(yunshi[j]).width);
//		var h = parseInt(window.getComputedStyle(yunshi[j]).height);
//		var ftop = parseInt(window.getComputedStyle(feiji).top);
//		var fleft = parseInt(window.getComputedStyle(feiji).left);
//		var fw = parseInt(window.getComputedStyle(feiji).width);
//		var fh = parseInt(window.getComputedStyle(feiji).height);
//		
		var fcenter = {
			x : parseInt(window.getComputedStyle(feiji).left) + parseInt(window.getComputedStyle(feiji).width)/2,
			y : parseInt(window.getComputedStyle(feiji).top) + parseInt(window.getComputedStyle(feiji).height)/2
		};
		var ycenter = {
			x : parseInt(window.getComputedStyle(yunshi[j]).left) + parseInt(window.getComputedStyle(yunshi[j]).width)/2,
			y : parseInt(window.getComputedStyle(yunshi[j]).top) + parseInt(window.getComputedStyle(yunshi[j]).height)/2
		};
		var fr = parseInt(window.getComputedStyle(feiji).height)/2 - 10;
		var yr = parseInt(window.getComputedStyle(yunshi[j]).width)/2 - 25;
		
//		if((ftop+fh>Top) &&(ftop<= (h+Top))&&( ( fleft<=(w+Left) ) && ( (fleft+fw)>=Left) ) ){
//			return true;
//		}
//		else{
//			continue;
//		}

		if( (Math.abs(fcenter.x - ycenter.x) <= ( fr + yr ) ) && ( (Math.abs(fcenter.y - ycenter.y) <= ( fr + yr ) ) )){
			return true;
		}
		else{
			continue;
		}
	}
}


//拾取能量
function shiqu(){
	for(let j=nengliang.length-1;j>=0;j--){
//		var Top = parseInt(window.getComputedStyle(nengliang[j]).top);
//		var Left = parseInt(window.getComputedStyle(nengliang[j]).left);
//		var w = parseInt(window.getComputedStyle(nengliang[j]).width) -50;
//		var h = parseInt(window.getComputedStyle(nengliang[j]).height) -50;
//		var ftop = parseInt(window.getComputedStyle(feiji).top);
//		var fleft = parseInt(window.getComputedStyle(feiji).left);
//		var fw = parseInt(window.getComputedStyle(feiji).width) -10;
//		var fh = parseInt(window.getComputedStyle(feiji).height) -10;
//		
		var fcenter = {
			x : parseInt(window.getComputedStyle(feiji).left) + parseInt(window.getComputedStyle(feiji).width)/2,
			y : parseInt(window.getComputedStyle(feiji).top) + parseInt(window.getComputedStyle(feiji).height)/2
		};
		var ncenter = {
			x : parseInt(window.getComputedStyle(nengliang[j]).left) + parseInt(window.getComputedStyle(nengliang[j]).width)/2,
			y : parseInt(window.getComputedStyle(nengliang[j]).top) + parseInt(window.getComputedStyle(nengliang[j]).height)/2
		};
		var fr = parseInt(window.getComputedStyle(feiji).height)/2 - 10;
		var nr = parseInt(window.getComputedStyle(nengliang[j]).width)/2 - 25;
		
		
		
//		if((ftop+fh>Top) &&(ftop<= (h+Top))&&( ( fleft<=(w+Left) ) && ( (fleft+fw)>=Left) ) ){
//			nengliang[j].remove();
//			return true;
//		}
//		else{
//			continue;
//		}

		if( (Math.abs(fcenter.x - ncenter.x) <= ( fr + nr ) ) && ( (Math.abs(fcenter.y - ncenter.y) <= ( fr + nr ) ) )){
			nengliang[j].remove();
			return true;
		}
		else{
			continue;
		}

	}
}


var chakan = document.getElementsByClassName("wuzi-chakan");

for(let cha=0;cha<chakan.length;cha++){
	chakan[cha].addEventListener("click",function(){
		console.log();
		var qiangqing = document.getElementsByClassName("qiangqing")[0]
		var innh = this.previousElementSibling.innerHTML;
		if(innh=="泰迪熊公仔"){
			qiangqing.getElementsByClassName("qiangqing-text1")[0].innerHTML = "sinomax 泰迪熊公仔";
			qiangqing.getElementsByClassName("qiangqing-text2")[0].innerHTML = "sinomax与teddy bear collection联手推出的萌熊公仔，卖萌减压，你我爱他。";
			qiangqing.getElementsByTagName("img")[0].src = "img/game/game-chakan-pic1.png";
		}
		else if(innh=="悠悠午睡枕"){
			qiangqing.getElementsByClassName("qiangqing-text1")[0].innerHTML = "sinomax 悠悠午睡枕";
			qiangqing.getElementsByClassName("qiangqing-text2")[0].innerHTML = "午睡神器，高效充电。适合办公室午休，也可充当腰垫。";
			qiangqing.getElementsByTagName("img")[0].src = "img/game/game-chakan-pic2.png";
		}
		else if(innh=="珍珠太空枕"){
			qiangqing.getElementsByClassName("qiangqing-text1")[0].innerHTML = "sinomax 珍珠太空枕";
			qiangqing.getElementsByClassName("qiangqing-text2")[0].innerHTML = "人体工学造型，健康颈椎承托。不管是仰卧还是侧卧，都有安适好睡眠。";
			qiangqing.getElementsByTagName("img")[0].src = "img/game/game-chakan-pic3.png";
		}
		else if(innh=="太空船模型"){
			qiangqing.getElementsByClassName("qiangqing-text1")[0].innerHTML = "sinomax 太空船模型";
			qiangqing.getElementsByClassName("qiangqing-text2")[0].innerHTML = "山猫太空船正版模型，带着梦想一飞冲天。";
			qiangqing.getElementsByTagName("img")[0].src = "img/game/game-chakan-pic4.png";
		}
		else if(innh=="舒适透气薄垫"){
			qiangqing.getElementsByClassName("qiangqing-text1")[0].innerHTML = "sinomax 舒适透气薄垫";
			qiangqing.getElementsByClassName("qiangqing-text2")[0].innerHTML = "舒适触感和健康脊椎承托，带给全家人温馨享受和整晚安适睡眠。";
			qiangqing.getElementsByTagName("img")[0].src = "img/game/game-chakan-pic5.png";
		}
		else if(innh=="梦六方床垫"){
			qiangqing.getElementsByClassName("qiangqing-text1")[0].innerHTML = "sinomax 梦六方床垫";
			qiangqing.getElementsByClassName("qiangqing-text2")[0].innerHTML = "开创业内纯海绵组合床垫先河，玩魔方一样自由搭配六块海绵，软硬可调。";
			qiangqing.getElementsByTagName("img")[0].src = "img/game/game-chakan-pic6.png";
		}
		
		
		qiangqing.classList.add("qiangqinga");
		
	})
}

var qiangqingguanbi = document.getElementsByClassName("qiangqing-guanbi")[0];
qiangqingguanbi.addEventListener("click",function(){
	this.parentNode.classList.remove("qiangqinga");
})


function gameinit(){
	
	var zailai = document.getElementsByClassName("zailai")[0];
	var fanhui = document.getElementsByClassName("fanhui")[0];
	var xiayiguan = document.getElementsByClassName("xiayi1")[0];
	
	fanhui.addEventListener("click",function(){
		window.location = "index.html";
	})

	//game1
	if(document.getElementsByClassName("game-tip")[0]){
		document.getElementsByClassName("game-tip")[0].classList.add("game-tipa");
		zailai.addEventListener("click",function(){
			window.location = "game1.html";
		})
		xiayiguan.addEventListener("click",function(){
			window.location = "game2.html";
		})
	}
	//game2
	if(document.getElementById("game2")){
		document.getElementById("game2").classList.add("game2-starta");
		zailai.addEventListener("click",function(){
			window.location = "game2.html";
		})
		xiayiguan.addEventListener("click",function(){
			window.location = "game3.html";
		})
	}
	//game3
	if(document.getElementById("game3")){
		document.getElementById("game3").classList.add("game2-starta");
		zailai.addEventListener("click",function(){
			window.location = "game3.html";
		})
		xiayiguan.addEventListener("click",function(){
			window.location = "choujiang.html";
		})
	}
	
	game1.addEventListener("touchstart",function(e){
		e.preventDefault();
		e.stopPropagation();
		feiji.classList.add("game-feijim");
		feijimovem(e);
	});
	
	game1.addEventListener("touchmove",function(e){
		e.preventDefault();
		e.stopPropagation();
		if(feiji.classList.contains("game-feijim")){
			var feijimt = setTimeout(function(){
				feiji.classList.remove("game-feijim");
				clearTimeout(feijimt);
			},300);
		}
		feijimovem(e);
	})
	
	game1.addEventListener("touchcancel",function(){
		feijimovem();
	});
	
	//碰撞检测
	var peng = setInterval(function(){
		if(shiqu()){
			var countnum = document.getElementsByClassName("countnum")[0];
			countnum.innerHTML = (parseInt(countnum.innerHTML)) + 1;
		}
		if(pengzhuang()){
			document.getElementsByClassName("shibai")[0].classList.add("shibaia");
			clearTimeout(succestime);
			clearInterval(peng);
		}
	},10);
	
	
	//陨石下落
	var yuns = [],yunshileng = yunshi.length;
	for(let i=yunshi.length-1;i>=0;i--){
		yuns[i] = setTimeout(function(){
			yunshimove(yunshi[i]);
		},3000*(yunshileng - i));
	}
	
	//能量下落
	var nengl = [];
	for(let i=nengliang.length-1;i>=0;i--){
		nengl[i] = setTimeout(function(){
			nengliangmove(nengliang[i]);
			clearTimeout(nengl[i]);
		},(2000*(Math.random()+2))*(-i+4));
	}
	
	
	//25s后关卡完成
	succestime = setTimeout(function(){
		if(((parseInt(document.getElementsByClassName("countnum")[0].innerHTML)==3)&&(document.getElementsByClassName("game-tip")[0]))||((parseInt(document.getElementsByClassName("countnum")[0].innerHTML)==2)&&(document.getElementsByClassName("game2-start")[0]))||((parseInt(document.getElementsByClassName("countnum")[0].innerHTML)==1)&&(document.getElementById("game3")))){
			document.getElementsByClassName("succes")[0].classList.add("succesa");
			if(document.getElementsByClassName("fenxiang")[0]){
				document.getElementsByClassName("fenxiang")[0].classList.add("fenxianga");
			}
			clearTimeout(succestime);
		}
		else{
			document.getElementsByClassName("shibai")[0].classList.add("shibaia");
			clearTimeout(succestime);
		}
	},40000);
	
	
}

//game1
if(document.getElementsByClassName("game-tip-liaojie")[0]){
	var liaojie = document.getElementsByClassName("game-tip-liaojie")[0];
	liaojie.addEventListener("touchstart",gameinit);
	
}

//game2
if(document.getElementById("game2")){
	var game2_start = document.getElementById("game2");
	game2_start.addEventListener("touchstart",gameinit);
}

//game3
if(document.getElementById("game3")){
	var game3_start = document.getElementById("game3");
	game3_start.addEventListener("touchstart",gameinit);
}

//fenxiang
if(document.getElementsByClassName("fenxiang")[0]){
	document.getElementsByClassName("fenxiang")[0].addEventListener("click",function(){
		this.remove();
	})
}
